<template>
  <div class="login">
    <p>管理更简单，执行更高效</p>
    <div class="login-box">
      <div class="login-left">
        <div class="logo">
          <img src="../../assets/img/login/login.png" alt="">
        </div>
        <el-form class="loginForm" ref="loginForm" :model="loginForm" :rules="ruleLogin">
          <el-form-item prop="phone" class="loginForm-item">
            <div class="i ic_tel_nor"></div>
            <el-input type="text" placeholder="请输入手机号" v-model="loginForm.phone"
            ></el-input>
          </el-form-item>
          <el-form-item class="loginForm-item" prop="password">
            <div class="i ic_lock_nor"></div>
            <el-input type="password" placeholder="请输入密码" v-model="loginForm.password"
                      autocomplete="new-password"
                      @keyup.enter.native="loginSubmit('loginForm')" :maxlength="16"></el-input>
          </el-form-item>
          <el-form-item class="loginForm-item" style="margin-bottom:20px;">
            <div class="login-btn" @click="loginFn('loginForm')">登录</div>
          </el-form-item>
        </el-form>
        <div class="login-tips">
          <div @click="toRegister">注册</div>
          <div @click="toForgetPassword">忘记密码?</div>
        </div>
      </div>
      <div class="login-right"></div>
    </div>
  </div>
</template>

<script>
  export default {
    name: "Login",
    data() {
      const validatePhone = (rule, value, callback) => {
        var reg = /^1\d{10}$/;
        if (value === '') {
          callback(new Error('请输入手机号'));
        } else if (!reg.test(value)) {
          callback(new Error('请输入正确的手机号'));
        } else {
          callback();
        }
      };
      return {
        ruleLogin: { //登录表单验证
          phone: [
            {required: true, validator: validatePhone, trigger: 'blur'}
          ],
          password: [
            {required: true, message: '请输入密码', trigger: 'blur'}
          ]
        },
        loginForm: { //登录表单
          phone: '',
          password: '',
        },
      }
    },
    methods:{
      queryCountUnRead(){
        this.$http.get('user/user/push/msg/countUnRead',{params:{
            userId:this.$store.state.userInfo.userId,
          }}).then(res=>{
          if(res.statusCode==200){
            this.$store.commit('SET_COUNT_UNREAD', res.data);
          }else {
          }
        });
      },
      toRegister(){
        this.$router.push({path: '/register'});
      },
      toForgetPassword(){
        this.$router.push({path: '/forgetPassword'});
      },
      loginFn(name){
        let _this=this;
        this.$refs[name].validate((valid)=>{
          _this.$http.post("/user/user/login",this.loginForm)
            .then(res =>{
              if(res.statusCode==200){
                _this.$store.commit('SET_USERINFO', res.data);
                _this.$router.push({path: '/smart'});
                _this.queryCountUnRead();
              }else {
                _this.$message.error( res.statusMsg);
              }
            }).catch(err =>{
            console.log(err)
          })
        });
      },
    }
  }
</script>

<style scoped type="text/scss" lang="scss">
  .login {
    width: 1265px;
    height: 755px;
    background-color: #FFFFFF;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;

    > p {
      text-align: right;
      font-size: 16px;
      color: #7392AD;
      letter-spacing: 10px;
      padding: 113px 78px 122px 0;
    }

    .login-box {
      display: flex;
      padding-right: 37px;

      .login-left {
        flex: 1;

        .logo {
          /*background-image: url("../../assets/img/login/login.png");*/
          background-position: center;
          background-repeat: no-repeat;
          width: 245px;
          height: 51px;
          background-size: cover;
          margin: 0 auto;
          img{
            display: block;
            width: 100%;
          }
        }

        .loginForm {
          padding-top: 60px;

          .loginForm-item {
            width: 362px;
            margin: 0 auto;
            margin-bottom: 30px;
            position: relative;

            .el-input__inner {
              padding: 0 45px;
            }

            .i {
              position: absolute;
              width: 18px;
              height: 18px;
              left: 12px;
              top: 14px;
              z-index: 10;
              background-size: 18px 18px;
            }

            .ic_tel_nor {
              background-image: url("../../assets/img/login/ic_tel_nor.png");
            }

            .ic_lock_nor {
              background-image: url("../../assets/img/login/ic_lock_nor.png");
            }
          }

          .login-btn {
            margin-top: 17px;
            width: 362px;
            height: 56px;
            line-height: 56px;
            text-align: center;
            color: #FFFFFF;
            font-size: 16px;
            background-color: #1192fa;
            border-radius: 6px;
            cursor: pointer;
          }
        }

        .login-tips {
          display: flex;
          width: 362px;
          margin: 0 auto;
          > div {
            flex: 1;
            font-size: 14px;
            cursor: pointer;
          }

          > div:nth-child(1) {
            color: #1192fa;
          }

          > div:nth-child(2) {
            color: #999999;
            text-align: right;
          }
        }
      }

      .login-right {
        background-image: url("../../assets/img/login/login_bg_03.png");
        background-size: cover;
        background-repeat: no-repeat;
        background-position: center;
        width: 614px;
        height: 437px;
      }
    }
  }
</style>
<style>
  .login .el-input__inner {
    padding: 0 45px;
    line-height: 44px;
    height: 44px;
    font-size: 16px;
  }
</style>
